package com.googlecode.wmlynar.gwtbeans.client.beans;

import java.util.ArrayList;
import java.util.List;

/**
 * Containst group of bindings. Allows to for example perform bind and unbind
 * operations on all bindings in the group.
 * 
 * @author Wojciech Mlynarczyk (wmlynar@gmail.com)
 * 
 */
public class BindingGroup {

    private final List<Binding> bindings = new ArrayList<Binding>();

    /**
     * Add binding to the group.
     * 
     * @param binding
     *            Binding to be added.
     */
    public void add(final Binding binding) {
        bindings.add(binding);
    }

    /**
     * Remove binding from the group.
     * 
     * @param binding
     *            Binding to be removed.
     */
    public void remove(final Binding binding) {
        bindings.remove(binding);
    }

    /**
     * Binds all the bindings contained in the group.
     */
    public void bind() {
        for (final Binding b : bindings) {
            b.bind();
        }
    }

    /**
     * Unbinds all the bindings contained in the group.
     */
    public void unbind() {
        for (final Binding b : bindings) {
            b.unbind();
        }
    }

    /**
     * Copies values of the last property in the chain from for all the bindings
     * in the chain from object A to B.
     */
    public void copyAToB() {
        for (final Binding b : bindings) {
            b.copyAToB();
        }
    }

    /**
     * Copies values of the last property in the chain from for all the bindings
     * in the chain from object B to A.
     */
    public void copyBToA() {
        for (final Binding b : bindings) {
            b.copyBToA();
        }
    }

}
